package farmer.gui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import farmer.render.building.Map;
import farmer.render.building.Road;
import farmer.render.unit.Transporter;
import farmer.render.unit.Worker;

import farmer.core.Building;
import farmer.core.Unit;
import farmer.game.MainGameState;
import core.gui.Component;

public class Minimap extends Component {
	
	private Map map;
	private MainGameState state;
	final int cellSize = 3;
	private Color grassWihtAlpha = new Color(95, 211, 24, 175);
	
	public Minimap(Map map, MainGameState state) {
		this.map = map;
		this.state = state;
		boundingRect.setFrame(800-(cellSize*51)-8,8,51*cellSize,51*cellSize);
	}
	
	public void update(int deltaTime) {}
	
	//Update the map position based on click on the minimap
	public void mousePressed(MouseEvent e){
		if(e.getButton() == MouseEvent.BUTTON1){
			if(boundingRect.contains(e.getX(), e.getY())){
				int x = (int) (e.getX()-boundingRect.getX());
				int y = (int) (e.getY()-boundingRect.getY());
				int cellX = x/cellSize - 400/Map.cellWidth;
				int cellY = y/cellSize - 300/Map.cellWidth;
				int translateX = cellX*Map.cellWidth;
				int translateY = cellY*Map.cellWidth;
				state.translateX = -translateX;
				state.translateY = -translateY;
			}
		}
	}
	
	public void render(Graphics2D g) {

		final int X = (int) boundingRect.getX();
		final int Y = (int) boundingRect.getY();
		final int WIDTH = (int) boundingRect.getWidth();
		final int HEIGHT = (int) boundingRect.getHeight();

		g.setColor(new Color(0,0,0,125));
		g.fillRect(X-2, Y-2, WIDTH+4, HEIGHT+4);
		g.setColor(grassWihtAlpha);
		g.fillRect(X,Y,WIDTH,HEIGHT);

		g.setColor(Color.black);

		//Fill all cells.
		for(int x = 0; x < map.getSizeX(); x++) {
			for(int y = 0; y < map.getSizeY(); y++) {
				int type = map.getConstructionMapValue(x, y);
				switch(type) {
				case Map.empty:
					if(x == 0|| x == map.getSizeX()-1 || y == 0 || y == map.getSizeY()-1){
						continue;
					}else{
						continue;
					}
				case Map.full:
					g.setColor(Color.white);
					break;
				case Map.city:
					g.setColor(Color.white);
					break;
				case Map.farm:
					g.setColor(Color.white);
					break;
				case Map.road:
					g.setColor(new Color(138, 99, 6));
					break;
				case Map.tree:
					g.setColor(new Color(15, 87, 20));
					break;
				case Map.rock:
					g.setColor(new Color(83, 83, 83));
					break;
				case Map.door:
					g.setColor(Color.white);
					break;
				default:
					continue;
				}

				g.fillRect(X + x*cellSize, Y + y*cellSize, cellSize, cellSize);
			}
		}

		//Outline all buildings
		g.setColor(Color.black);
		for(Building b : map.getBuildingList()){
			if(b instanceof Road){
				continue;
			}
			g.drawRect(X + b.getCellX()*cellSize, Y + b.getCellY()*cellSize, b.getSize()*cellSize, b.getSize()*cellSize);
		}

		//Draw all units
		for(Unit unit : map.getUnitList()) {
			int x = (int)Math.round(cellSize * (unit.getX()-14)/(double)Map.cellWidth);
			int y = (int)Math.round(cellSize * (unit.getY()-14)/(double)Map.cellWidth);

			if(unit instanceof Worker){
				g.setColor(Color.blue);
			}else if(unit instanceof Transporter){
				g.setColor(Color.red);
			}

			g.fillRect(X + x, Y + y, 2, 2);
		}
		
		//Draw vision rectangle
		int posX = X + (int) -(state.translateX)/Map.cellWidth*cellSize;
		int posY = Y + (int) -(state.translateY)/Map.cellWidth*cellSize;
		int width = MainGameState.width*cellSize/Map.cellWidth;
		int height = MainGameState.height*cellSize/Map.cellWidth;
		g.setColor(new Color(29, 220, 241, 150));
		g.drawRect(posX, posY, width, height);
	}
}
